package com.ldj.struts.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.common.SystemConfig;
import com.ldj.common.SystemSession;
import com.ldj.hibernate.AnnualVeri;
import com.ldj.hibernate.AnnualVeriDAO;
import com.ldj.hibernate.AnnualVeriRepEndDate;
import com.ldj.hibernate.AnnualVeriRepEndDateDAO;
import com.ldj.hibernate.CaseInfo;
import com.ldj.hibernate.CaseInfoDAO;
import com.ldj.hibernate.EmployerInfo;
import com.ldj.hibernate.EmployerInfoDAO;
import com.ldj.hibernate.PersionFile;
import com.ldj.hibernate.SysLogInfo;
import com.ldj.hibernate.SysLogInfoDAO;
import com.ldj.hibernate.UnitUser;
import com.ldj.hibernate.UnitUserDAO;
import com.ldj.struts.form.AnnualVeriForm;

public class AnnualVeriAction extends DispatchAction {

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
	    String annualVeriYear = request.getParameter("annualVeriYear");
	    String annualVeriState = request.getParameter("annualVeriState");
	    String empInfoName = request.getParameter("empInfoName");
		AnnualVeriDAO dao = new AnnualVeriDAO(request, response);
		dao.getSession().flush();
		dao.getSession().clear();
		List list = dao.findAllPage(annualVeriYear, annualVeriState, empInfoName);
		request.setAttribute("list", list);
		String year=new SimpleDateFormat("yyyy").format(Calendar.getInstance().getTime());
		int y = Integer.parseInt(year); 
		int startYear = y-10;
		int endYear = y+2;
		request.setAttribute("startYear", startYear);
		request.setAttribute("endYear", endYear);
		
		return mapping.findForward("list");
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AnnualVeriDAO dao = new AnnualVeriDAO(request, response);
		dao.getSession().flush();
		dao.getSession().clear();
		AnnualVeri av = dao.findById(request.getParameter("aid"));
		if(av.getAnnualVeriApprovalerId() != null){
			CaseInfoDAO dao1 = new CaseInfoDAO(request, response);
			PersionFile p1 = dao1.findPersionFileById(av.getAnnualVeriApprovalerId());
			request.setAttribute("p1", p1);
		}
		request.setAttribute("av", av);
		return mapping.findForward("detail");
	}
	
	public ActionForward toApply(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AnnualVeriDAO dao = new AnnualVeriDAO(request, response);
		dao.getSession().flush();
		dao.getSession().clear();
		AnnualVeri av = dao.findById(request.getParameter("aid"));
		request.setAttribute("av", av);
		if(av.getAnnualVeriApprovalerId() != null && !"".equals(av.getAnnualVeriApprovalerId())){
			CaseInfoDAO dao1 = new CaseInfoDAO(request, response);
			request.setAttribute("p1", dao1.findPersionFileById(av.getAnnualVeriApprovalerId()));
		}
		return mapping.findForward("toApply");
	}
	
	public ActionForward apply(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AnnualVeriForm aForm = (AnnualVeriForm)form;
		AnnualVeriDAO dao = new AnnualVeriDAO(request, response);
		dao.getSession().flush();
		dao.getSession().clear();
		AnnualVeri av = dao.findById(request.getParameter("aid"));
		av.setAnnualVeriApprovalDate(new Date());
		av.setAnnualVeriState(aForm.getAnnualVeriState());
		av.setAnnualVeriApprovalerId(SystemSession.getSupUserId(request));
		av.setAnnualVeriRemark(aForm.getAnnualVeriRemark());
		av.setAnnualVeriHonesLevel(aForm.getAnnualVeriHonesLevel());
		EmployerInfoDAO dao1 = new EmployerInfoDAO(request, response);
		EmployerInfo info = dao1.findById(av.getEmployerInfo().getEmpInfoId());
		info.setEmpInfoHonesLevel(av.getAnnualVeriHonesLevel());
		dao.merge(av);
		dao1.merge(info);
		
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoOperType("9");
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "AnnualVeri").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", av.getEmployerInfo().getEmpInfoName()));
		logDao.save(s);
		return new ActionForward("/AnnualVeri.do?method=list", true);
	}
	
	public ActionForward avDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		//获取年审申报截止日期表中的年度与年审表中的年度字段，进行判断是否可以申请年审
		AnnualVeriRepEndDateDAO dao = new AnnualVeriRepEndDateDAO(request, response);
		AnnualVeriRepEndDate annuEndDate = dao.find();
       //获取单位id
		String unitId = request.getParameter("eid");
		String year = annuEndDate.getAnnualVeriRepEndDateYear();
		AnnualVeriDAO annuVeridao = new AnnualVeriDAO(request, response);
		List list = annuVeridao.findByYear(year, unitId);
		AnnualVeri av = (AnnualVeri) list.get(0);
		request.setAttribute("av", av);
		return mapping.findForward("avDetail");
	
	}
	
	
	
	public ActionForward toCase(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		AnnualVeriDAO dao = new AnnualVeriDAO(request, response);
		dao.getSession().flush();
		dao.getSession().clear();
		AnnualVeri av = dao.findById(request.getParameter("aid"));
		av.setAnnualVeriIsCase("1");
		dao.merge(av);
		CaseInfo cInfo = new CaseInfo();
		cInfo.setEmployerInfo(av.getEmployerInfo());
		cInfo.setCaseInfoBasiccondiBeginres(av.getAnnualVeriRemark());
		cInfo.setCaseInfoSource("005003");
		request.setAttribute("cInfo", cInfo);
		SysLogInfoDAO logDao = new SysLogInfoDAO(null, null);
		SysLogInfo s = new SysLogInfo();
		s.setPersionFile(SystemSession.getSupUserInfo(request));
		s.setSysLogInfoOperDate(new Date());
		s.setSysLogInfoSupOrganCode(SystemSession.getSupUserInfo(request).getPerFileOrganCode());
		s.setSysLogInfoOperType("11");
		s.setSysLogInfoOperRecord(SystemConfig.getString("syslog", "AnnualVeri.case").replace("$user", SystemSession.getSupUserInfo(request).getPerFileName()).replace("$record", av.getEmployerInfo().getEmpInfoName()));
		logDao.save(s);
		return mapping.findForward("toCase");
	}
}